#ifndef _NODE_H
#define _NODE_H

#include <vector>
#include "opencv/cv.h"

class Node
{
public:
	Node();
	Node(int x1, int y1, int x2, int y2, int level , Node *ancestor);
	~Node();
	
	void RemoveAdjacentMatrix();
	
	const int GetTlX() const { return tl_x; }
	const int GetTlY() const { return tl_y; }
	const int GetBrX() const { return br_x; }
	const int GetBrY() const { return br_y; }
	const int GetNodeLevel() const { return nodeLevel; }
	
	const int GetColorAverage() const { return colorAverage; }
	const int GetColorAverageBlue() const { return colorAverageBlue; }
	const int GetColorAverageGreen() const { return colorAverageGreen; }
	const int GetColorAverageRed() const { return colorAverageRed; }
	
	const bool GetColored() const { return colored; }
	const Node* GetNodeAncestor() const { return nodeAncestor; }
	
	void SetColored(bool isColored) { colored = isColored; }
	
	std::vector<Node*>& ReturnAdjacentMatrixTop() { return adjacentMatrix[0]; }
	std::vector<Node*>& ReturnAdjacentMatrixRight() { return adjacentMatrix[1]; }
	std::vector<Node*>& ReturnAdjacentMatrixBottom() { return adjacentMatrix[2]; }
	std::vector<Node*>& ReturnAdjacentMatrixLeft() { return adjacentMatrix[3]; }
	std::vector<Node*>& ReturnAdjacentMatrix(int side) { return adjacentMatrix[side]; }
	
	bool IsHomogeneous(IplImage *img, int threshold, int minSize = 1);
	bool IsHomogeneousDeviation(IplImage *img, int threshold, int minSize = 1);
	int CalculateNbPixels();
	void CalculateColorAverage(IplImage *img);
	void ColorNode(IplImage *img, CvScalar pixel, int averageMax, int averageMin);
	
	// Side a true : on est a droite ou en haut dans sa partie.
	// Side à false : on est a gauche ou en bas dans sa partie.
	void InheritAdjacentMatrixTop(std::vector<Node*> &top, bool side = true);
	void InheritAdjacentMatrixRight(std::vector<Node*> &right, bool side = true);
	void InheritAdjacentMatrixBottom(std::vector<Node*> &bottom, bool side = true);
	void InheritAdjacentMatrixLeft(std::vector<Node*> &left, bool side = true);
	
	void AddNeighbourTop(Node *neighbour);
	void AddNeighbourRight(Node *neighbour);
	void AddNeighbourBottom(Node *neighbour);
	void AddNeighbourLeft(Node *neighbour);
	
private:
	int tl_x;
	int tl_y;
	int br_x;
	int br_y;
	int nodeLevel;
	int colorAverage;
	int colorAverageBlue;
	int colorAverageGreen;
	int colorAverageRed;
	
	bool colored;
	Node *nodeAncestor;
	std::vector<Node*> adjacentMatrix[4];
};

#endif // _NODE_H
